#include "columnwidget.h"
#include<QLabel>
#include<QToolButton>
#include<QEvent>
#include<QEnterEvent>
#include<QPaintEvent>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QSpacerItem>
#include<QStyle>
#include<QPainter>
#include<QStyleOption>
#include<QGridLayout>

ColumnWidget::ColumnWidget(const QString &title, QWidget *parent)
    : QWidget{parent}
{
    this->title_label=new QLabel(title,this);
    this->play_toolButton=new QToolButton(this);
    this->adjust_toolButton=new QToolButton(this);
    this->refresh_toolButton=new QToolButton(this);
    this->more_toolButton=new QToolButton(this);
    this->column_widget=new QWidget;

    this->title_label->setObjectName("title_label");
    this->play_toolButton->setObjectName("play_toolButton");
    this->adjust_toolButton->setObjectName("adjust_toolButton");
    this->refresh_toolButton->setObjectName("refresh_toolButton");
    this->more_toolButton->setObjectName("more_toolButton");
    this->column_widget->setObjectName("column_widget");

    this->play_toolButton->setIcon(QIcon("://image/tabIcon/play2-gray.svg"));
    this->adjust_toolButton->setIcon(QIcon("://image/tabIcon/adjust-column-gray.svg"));
    this->refresh_toolButton->setIcon(QIcon("://image/tabIcon/refresh-gray.svg"));
    this->more_toolButton->setIcon(QIcon("://image/tabIcon/more2-gray.svg"));

    this->play_toolButton->setIconSize(QSize(24,24));

    this->setStyleSheet("QLabel#title_label{font-size: 20px;} QToolButton{background-color:rgba(255,255,255,0%)}");


    layoutUI();

    this->adjust_toolButton->hide();

}

void ColumnWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void ColumnWidget::enterEvent(QEnterEvent *event)
{
    this->adjust_toolButton->show();
}

void ColumnWidget::leaveEvent(QEvent *event)
{
    this->adjust_toolButton->hide();
}

void ColumnWidget::layoutUI()
{
    QHBoxLayout* hLayout=new QHBoxLayout;
    hLayout->addWidget(this->title_label);
    hLayout->addWidget(this->play_toolButton);
    hLayout->addSpacerItem(new QSpacerItem(40,20,QSizePolicy::Expanding));
    hLayout->addWidget(this->adjust_toolButton);
    hLayout->addWidget(this->refresh_toolButton);
    hLayout->addWidget(this->more_toolButton);


    QGridLayout* gLayout=new QGridLayout(this->column_widget);
    for(int i=0;i<3;++i)
    {
        for(int j=0;j<3;++j)
        {
            auto item=new ItemListWidget(QPixmap("://image/tabIcon/music-cover.jpg"),"歌曲名","作者",this->column_widget);
            gLayout->addWidget(item,i,j);
        }
    }


    QVBoxLayout* vLayout=new QVBoxLayout(this);
    vLayout->addLayout(hLayout);
    vLayout->addWidget(this->column_widget);
}

ItemListWidget::ItemListWidget(const QPixmap &coverPix, const QString &name, const QString &author, QWidget *parent)
    : QWidget{parent}
{
    this->cover_label=new QLabel(this);
    this->name_label=new QLabel(name,this);
    this->author_label=new QLabel(author,this);
    this->play_toolButton=new QToolButton(this);
    this->like_toolButton=new QToolButton(this);
    this->more_toolButton=new QToolButton(this);

    this->cover_label->setObjectName("cover_label");
    this->name_label->setObjectName("name_label");
    this->author_label->setObjectName("author_label");
    this->play_toolButton->setObjectName("play_toolButton");
    this->like_toolButton->setObjectName("like_toolButton");
    this->more_toolButton->setObjectName("more_toolButton");

    this->cover_label->setFixedSize(60,60);
    this->cover_label->setPixmap(coverPix);
    this->cover_label->setScaledContents(true);

    this->play_toolButton->setIcon(QIcon("://image/tabIcon/play3-gray.svg"));
    this->like_toolButton->setIcon(QIcon("://image/playbar/collect.svg"));
    this->more_toolButton->setIcon(QIcon("://image/playbar/more.svg"));

    layoutUI();

    this->play_toolButton->hide();
    this->like_toolButton->hide();
    this->more_toolButton->hide();
}

void ItemListWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void ItemListWidget::enterEvent(QEnterEvent *event)
{
    this->play_toolButton->show();
    this->like_toolButton->show();
    this->more_toolButton->show();
}

void ItemListWidget::leaveEvent(QEvent *event)
{
    this->play_toolButton->hide();
    this->like_toolButton->hide();
    this->more_toolButton->hide();
}

void ItemListWidget::layoutUI()
{
    QVBoxLayout* vLayout=new QVBoxLayout;
    vLayout->addWidget(this->name_label);
    vLayout->addWidget(this->author_label);

    QHBoxLayout* hLayout=new QHBoxLayout(this);
    hLayout->addWidget(this->cover_label);
    hLayout->addLayout(vLayout);
    hLayout->addSpacerItem(new QSpacerItem(40,20,QSizePolicy::Expanding));
    hLayout->addWidget(this->play_toolButton);
    hLayout->addWidget(this->like_toolButton);
    hLayout->addWidget(this->more_toolButton);
}
